package com.wangtao.firstapp.fragments.homepagerfragmnets;

import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v7.widget.LinearLayoutManager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.google.gson.Gson;
import com.jcodecraeer.xrecyclerview.ProgressStyle;
import com.jcodecraeer.xrecyclerview.XRecyclerView;
import com.wangtao.firstapp.R;
import com.wangtao.firstapp.adapters.RVSifeAdapter;
import com.wangtao.firstapp.modles.SifeData;
import com.wangtao.firstapp.utils.HttpUtils;

import java.util.ArrayList;
import java.util.List;

/**
 * 礼物页面
 * Created by lanouhn on 16/8/24.
 */
public class GiftFragment extends Fragment{

    private XRecyclerView giftRecyclerView;//创建recyclerView
    private List<SifeData.DataBean.TopicBean> giftSifeData=new ArrayList<>();//创建实体类对象  !!!复用一个实体类

    private List<SifeData.DataBean.TopicBean> datas=new ArrayList<>();//中间值

    int page=0;

    private RVSifeAdapter giftRVSifeAdapter;//创建RVSifeAdapter适配器对象，！！！复用的


    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view=inflater.inflate(R.layout.fragment_sift_listview,container,false);
        giftRecyclerView= (XRecyclerView) view.findViewById(R.id.recyclerView_sift_listView);

        //上拉加载的动画效果
        giftRecyclerView.setRefreshProgressStyle(ProgressStyle.BallSpinFadeLoader);
        //recyclerView.setLaodingMoreProgressStyle(ProgressStyle.Pacman);
        //下拉刷新的动画效果
        giftRecyclerView.setArrowImageView(R.drawable.ic_loading_rotate);

        giftRecyclerView.setLoadingListener(new XRecyclerView.LoadingListener() {
            //下拉刷新
            @Override
            public void onRefresh() {
                page = 0;
                getDataFromWeb();
            }

            //上拉加载
            @Override
            public void onLoadMore() {
                page++;
                getDataFromWeb();
            }
        });

        getDataFromWeb();
        return view;
    }

    private void getDataFromWeb() {
        try {
             String url="http://open4.bantangapp.com/topics/topic/listByScene";
             String params="app_id=com.jzyd.BanTang&client_id=bt_app_android&client_secret=ffcda7a1c4ff338e05c42e7972ba7b8d&track_user_id=2560255&oauth_token=4df22230db195bba652ef0e593881122&track_deviceid=866479023806507&track_device_info=x600&channel_name=leshi&app_installtime=1471853508&app_versions=5.8.6&os_versions=5.0.2&screensize=1080&v=18&page="+page+"&pagesize=20&id=3";

            HttpUtils.doPostAsyn(url,params, new HttpUtils.CallBack() {
                @Override
                public void onRequestComplete(String result) {
                    Gson gson_gift=new Gson();
                    datas=gson_gift.fromJson(result,SifeData.class).getData().getTopic();
                    if (datas.size()>0){
                        handler.sendEmptyMessage(1);
                    }else {
                        handler.sendEmptyMessage(0);
                    }
                }
            });
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
    Handler handler=new Handler(){
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            switch (msg.what){
                case 0:
                    break;
                case 1:


                    if (page==0){//
                        //
                        giftSifeData.clear();
                        //
                        giftSifeData.addAll(datas);

                        giftRVSifeAdapter = new RVSifeAdapter(getActivity(), giftSifeData);
                        giftRecyclerView.setAdapter(giftRVSifeAdapter);
                        giftRecyclerView.setLayoutManager(new LinearLayoutManager(getActivity()));
                        //
                        giftRecyclerView.refreshComplete();
                        datas.clear();
                    }else {
                        giftSifeData.addAll(datas);
                        giftRVSifeAdapter.notifyDataSetChanged();
                        giftRecyclerView.loadMoreComplete();
                        datas.clear();
                    }

//                    giftRVSifeAdapter=new RVSifeAdapter(getActivity(),giftSifeData);
//                    giftRecyclerView.setAdapter(giftRVSifeAdapter);
//                    giftRecyclerView.setLayoutManager(new LinearLayoutManager(getActivity()));
                    break;
            }
        }
    };
}
